package com.immediatus.graphics.buffer;

import com.immediatus.graphics.texture.region.BaseTextureRegion;
import com.immediatus.graphics.vertex.RectangleBatchVertexBuffer;
import com.immediatus.util.FastFloatBuffer;

public class RectangleBatchTextureRegionBuffer extends BufferObject {
	private int _index;

	public RectangleBatchTextureRegionBuffer(final int capacity_, final int drawType_, final boolean managed_) {
		super(capacity_ * 2 * RectangleBatchVertexBuffer.VERTICES_PER_RECTANGLE, drawType_, managed_);
	}

	public int getIndex() {
		return this._index;
	}

	public void setIndex(final int index_) {
		this._index = index_;
	}

	public void add(final BaseTextureRegion textureRegion_) {
		final int x1 = Float.floatToRawIntBits(textureRegion_.getTextureCoordinateX1());
		final int y1 = Float.floatToRawIntBits(textureRegion_.getTextureCoordinateY1());
		final int x2 = Float.floatToRawIntBits(textureRegion_.getTextureCoordinateX2());
		final int y2 = Float.floatToRawIntBits(textureRegion_.getTextureCoordinateY2());

        int index = this._index;
		final int[] bufferData = this._BufferData;
		bufferData[index++] = x1;
		bufferData[index++] = y1;
		bufferData[index++] = x1;
		bufferData[index++] = y2;
		bufferData[index++] = x2;
		bufferData[index++] = y1;
		bufferData[index++] = x2;
		bufferData[index++] = y1;
		bufferData[index++] = x1;
		bufferData[index++] = y2;
		bufferData[index++] = x2;
		bufferData[index++] = y2;
		this._index = index;
	}

	public void submit() {
		final FastFloatBuffer buffer = this._FloatBuffer;
		buffer.position(0);
		buffer.put(this._BufferData);
		buffer.position(0);

		super.setHardwareBufferNeedsUpdate();
	}
}
